package com.ruibang.glass.quality.component.job;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruibang.glass.quality.constant.Constants;
import com.ruibang.glass.quality.entity.EverydayColdDirectLoss;
import com.ruibang.glass.quality.service.EverydayColdDirectService;
import com.ruibang.glass.quality.service.TotalEquDataService;
import com.teaming.cloud.framework2.elastic.job3.annotation.ElasticJobScheduler;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-30  09:24
 * @Description: TODO:冷端直投缺陷全检(每天)
 * @Version: 1.0
 */

@Slf4j
@ElasticJobScheduler(cron = "3 20 7 * * ?", name = "EverydayInvestmentDefectInspectionTask", shardingItemParameters = "0=0", jobParameters = "parameter")
//@ElasticJobScheduler(cron = "3 * * * * ?", failover = true, shardingTotalCount = 1, name = "EverydayInvestmentDefectInspectionTask", shardingItemParameters = "0=0", jobParameters = "parameter")
@Component
public class EverydayInvestmentDefectInspectionTask implements SimpleJob {

    @Autowired
    private EverydayColdDirectService everydayColdDirectService;

    @Autowired
    private TotalEquDataService totalEquDataService;

    @Override
    public void execute(ShardingContext shardingContext) {
        importEverydayColdDirect(Constants.DEFECT_DEFAULT_QUERY_DAYS);
    }

    @Transactional(rollbackFor = Exception.class)
    public void importEverydayColdDirect(int dayNum) {
        log.error("------------冷端直投缺陷全检(每天)开始----------------");
        for (int i = 0; i < dayNum; i++) {
            String today = LocalDate.now().minusDays(i).format(DateTimeFormatter.ISO_LOCAL_DATE);
            List<EverydayColdDirectLoss> old = everydayColdDirectService.queryTodayData(today);
            EverydayColdDirectLoss todayData = totalEquDataService.queryEverydayHourColdDirect(today);
            if (todayData != null) {
                long defectNum = todayData.getPlatinumTotalNum() + todayData.getBubbleTotalNum() + todayData.getStoneTotalNum();
                float platinumBoardNum = todayData.getPlatinumTotalNum() / (float) todayData.getCheckNum();
                float stoneBoardNum = todayData.getStoneTotalNum() / (float) todayData.getCheckNum();
                float bubbleBoardNum = todayData.getBubbleTotalNum() / (float) todayData.getCheckNum();
                float openBubbleBoardNum = todayData.getOpenBubbleTotalNum() / (float) todayData.getCheckNum();
                float defectBoardNum = defectNum / (float) todayData.getCheckNum();
                todayData.setDefectTotalNum(defectNum);
                todayData.setPlatinumBoardNum(platinumBoardNum);
                todayData.setStoneBoardNum(stoneBoardNum);
                todayData.setBubbleBoardNum(bubbleBoardNum);
                todayData.setDefectBoardNum(defectBoardNum);
                todayData.setOpenBubbleBoardNum(openBubbleBoardNum);
                if (CollectionUtils.isNotEmpty(old)) {
                    everydayColdDirectService.removeByIds(old.stream().map(EverydayColdDirectLoss::getId).collect(Collectors.toList()));
                }
                //保存
                everydayColdDirectService.save(todayData);
            }
        }
        log.error("------------冷端直投缺陷全检(每天)结束----------------");
    }
}
